package com.vcs.frm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.vcs.frm.dao.CommonDao;
import com.vcs.frm.dao.SchoolDao;
import com.vcs.frm.domain.Teacher;
import com.vcs.frm.domain.User;
import com.vcs.frm.utils.RoleConstants;
import com.vcs.frm.utils.UserNameUtil;

public class SchoolServiceImpl implements SchoolService {

	
	private SchoolDao schoolDao;
	
	@Autowired
	private CommonDao commonDao;
	
	public void setSchoolDao(SchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}
	
	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	public SchoolServiceImpl() {
	}
	
	@Override
	public int saveNewAdmissionDetails() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteTeacher(int teacherId) {
		int status = schoolDao.deleteTeacher(teacherId);
		return status;
	}

	@Override
	public List<Teacher> getAllTeachers() {
		List<Teacher> teachers= schoolDao.getAllTeachers();
		return teachers;
	}

	@Override
	public Teacher getTeacherByTeacherId(int teacherId) {
		Teacher teacher = schoolDao.getTeacherByTeacherId(teacherId);
		return teacher;
	}

	@Override
	@Transactional
	public int saveTeacher(Teacher teacher) {
		
		// get username for teacher, this will be inserted into USERS table
		String username = UserNameUtil.createUsername(teacher.getFirstName());
		System.out.println(username);
		List<String> roles = new ArrayList<String>();
		roles.add(RoleConstants.TEACHER);
		int id = commonDao.createUserAndSetRoles(username, roles);
		System.out.println("user id : "+ id );
		teacher.setTeacherId(id);
		int status = schoolDao.saveTeacher(teacher);
		return status;
	}

}
